﻿using AutoMapper;
using B.S.DataBasic.Domain.GoodInfra;
using B.S.DataBasic.Domain.RBAC;
using B.S.DataBasic.ErrorCode;
using B.S.DataBasic.Infrastructure;
using B.S.DataBasic.Infrastructure.Implement;
using B.S.DataBasic.Read.API.Dtos;
using MediatR;
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
using Microsoft.EntityFrameworkCore;

namespace B.S.DataBasic.Read.API.Application
{
    //public class LoginCommandHandler : IRequestHandler<LoginCommand, APIResult<User>>
    //{
    //    private readonly IBaseRepository<User> userRepository;
    //    private readonly IMapper mapper;
    //    public LoginCommandHandler(IBaseRepository<User> userRepository, IMapper mapper)
    //    {
    //        this.userRepository = userRepository;
    //        this.mapper = mapper;
    //    }

    //    public async Task<APIResult<User>> Handle(LoginCommand request, CancellationToken cancellationToken)
    //    {
    //        APIResult<User> result = new APIResult<User>();
    //        result.Code = EnumCode.登录成功;
    //        result.Message = EnumCode.登录成功.ToString();

    //        var list = userRepository.GetAll().Where(u => u.UserName == request.UserName);
    //        if (list.Count() != 1)
    //        {
    //            result.Code = EnumCode.登录失败;
    //            result.Message = EnumCode.登录失败.ToString();
    //            return await Task.FromResult(result);
    //        }

    //        var entity = list.FirstOrDefault();
    //        if (entity.Password != request.Password)
    //        {
    //            result.Code = EnumCode.登录失败;
    //            result.Message = EnumCode.登录失败.ToString();
    //            return await Task.FromResult(result);
    //        }

    //        result.Data = entity;

    //        return await Task.FromResult(result);
    //    }
    //}

    //public class LoginHandler : IRequestHandler<LoginRequest, User>
    //{
    //    private readonly DataDbContext _context;

    //    public LoginHandler(DataDbContext context)
    //    {
    //        _context = context;
    //    }

    //    public async Task<User> Handle(LoginRequest request, CancellationToken cancellationToken)
    //    {
    //        return await _context.User
    //           .Include(u => u.Role)
    //               .ThenInclude(r => r.RolePermissions)
    //                   .ThenInclude(rp => rp.Permission)
    //           .FirstOrDefaultAsync(u => u.Username == request.Username && u.Password == request.Password, cancellationToken);
    //    }
    //}
}
